package Test1;

import a1_waittandontify.cook1;
import a1_waittandontify.foodie1;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class lian_xi_Demo1 extends Thread {
    static Lock lock = new ReentrantLock();
    static int count = 100;

    /*
    有一百份礼品， 两个人同时送礼品，当剩下礼品小于10份时，停止送出，
    请用线程模拟这个过程 并将礼品名字和礼物数量打印出来
     */
    @Override
    /* 一个人送
    public void run() {
        while (count>10) {
            lock.lock();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+"送了第" + count + "份礼品");
                    count--;
               }
            lock.unlock();
        if(count<=10) {
            System.out.println("礼品剩下"+count+"份，不送了");
           }
        }*/

    //两个人送礼品
    public void run() {
        while (true) {
            lock.lock();
            if (count <= 10) {
                System.out.println("礼品剩下" + count + "份，不送了");
                break;
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "送了第" + count + "份礼品");
            count--;
            lock.unlock();
        }
    }
}



